package website.digitalocean.chapter7;

/**
 * @author: longhc@citycloud.com.cn
 * @since: 2023/2/10 10:23
 * @version: 1.0
 */
public class CommandExecutorProxy implements CommandExecutor {

    private boolean isAdmin;
    private CommandExecutor commandExecutor;

    public CommandExecutorProxy(String user, String pwd) {
        if ("lhc".equalsIgnoreCase(user) && "123".equalsIgnoreCase(pwd)) {
            isAdmin = true;
            commandExecutor = new CommandExecutorImpl();
        }
    }

    @Override
    public void runCommand(String command) throws Exception {
        if (isAdmin) {
            commandExecutor.runCommand(command);
            return;
        }
        if (command.trim().startsWith("rm")) {
            throw new RuntimeException("无效命令");
        } else {
            commandExecutor.runCommand(command);
        }
    }
}
